import 'package:flutter_base/common/utils/screen.dart';
import 'package:flutter_base/common/utils/utils.dart';
import 'package:flutter_base/common/values/values.dart';
import 'package:flutter_base/common/widgets/widgets.dart';
import 'package:flutter_base/pages/home/entitys/entrys.dart';
import 'package:flutter_base/pages/home/homeTab_page.dart';
import 'package:flutter_base/pages/home/widgets/search_bar.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;
  List<EntryEntity> _list = [
    EntryEntity(
        title: 'OA审批',
        color: Color(0xfff4a040),
        icon: Icon(
          Iconfont.shenpi,
          size: zhiSetWidth(18),
          color: Colors.white,
        )),
    EntryEntity(
        title: '云盘',
        color: Color(0xff4f95ed),
        icon: Icon(
          Iconfont.cloud,
          size: zhiSetWidth(18),
          color: Colors.white,
        )),
    EntryEntity(
        title: '图片',
        color: Color(0xfff4a040),
        icon: Icon(
          Icons.add_a_photo_outlined,
          size: zhiSetWidth(18),
          color: Colors.white,
        )),
    EntryEntity(
        title: '视频',
        color: Color(0xff57b988),
        icon: Icon(
          Icons.add_photo_alternate_outlined,
          size: zhiSetWidth(18),
          color: Colors.white,
        )),
    EntryEntity(
        title: '邮箱',
        color: Color(0xffdf604d),
        icon: Icon(
          Iconfont.email,
          size: zhiSetWidth(18),
          color: Colors.white,
        )),
    EntryEntity(
        title: '直播',
        color: Color(0xffee8430),
        icon: Icon(
          Iconfont.live,
          size: zhiSetWidth(18),
          color: Colors.white,
        )),
    EntryEntity(
        title: '视频会议',
        color: Color(0xff57b988),
        icon: Icon(
          Iconfont.shipintonghua,
          size: zhiSetWidth(18),
          color: Colors.white,
        )),
    EntryEntity(
        title: '报告',
        color: Color(0xffdf604d),
        icon: Icon(
          Iconfont.baogao,
          size: zhiSetWidth(18),
          color: Colors.white,
        )),
    EntryEntity(
        title: '用印',
        color: Color(0xff4f95ed),
        icon: Icon(
          Iconfont.shenpi,
          size: zhiSetWidth(18),
          color: Colors.white,
        )),
    EntryEntity(
        title: '更多',
        // color: Color(0xff57b988),
        icon: Icon(
          Iconfont.more,
          size: zhiSetWidth(24),
          color: Colors.blueAccent,
        ), color: Colors.white,),
  ];
  late TabController _tabController;
  late PageController _pageController;
  List<Tab> _tabList = [
    Tab(text: '最新信息'),
    Tab(text: '通知公告'),
    Tab(text: '收发文'),
    Tab(text: '会议纪要'),
    Tab(text: '信息交流')
  ];

  @override
  void initState() {
    _tabController = TabController(length: _tabList.length, vsync: this);
    _pageController = PageController();
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
          child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Row(
              children: [
                Text(
                  '协同办公平台',
                  style: TextStyle(fontSize: 20,fontWeight: FontWeight.w500),
                ),
                Icon(Icons.arrow_drop_down_sharp)
              ],
            ),
          ),
          Column(
            children: <Widget>[
              Container(
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        colors: [Color(0x66000000), Colors.transparent],
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter)),
                child: Container(
                  decoration: BoxDecoration(color: Colors.white),
                  child: SearchBar(
                    enabled: false,
                    hideLeft: false,
                    defaultText: '',
                    hint: '搜索',
                    inputMargin: EdgeInsets.fromLTRB(8, 0, 8, 0),
                  ),
                ),
              )
            ],
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 8.0),
            child: Row(
              children: [
                Expanded(
                    child: Container(
                  decoration: BoxDecoration(
                    border: Border(
                      right: BorderSide(color: Colors.grey[200]!, width: 1),
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Iconfont.daiban,
                        color: Colors.grey[600],
                        size: 16,
                      ),
                      SizedBox(
                        width: zhiSetWidth(3),
                      ),
                      Text(
                        '待办',
                        style: TextStyle(color: Colors.grey[600], fontSize: 14),
                        textAlign: TextAlign.center,
                      )
                    ],
                  ),
                )),
                Expanded(
                    child: Container(
                  decoration: BoxDecoration(
                    border: Border(
                      right: BorderSide(color: Colors.grey[200]!, width: 1),
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Iconfont.duban,
                        color: Colors.grey[600],
                        size: 20,
                      ),
                      SizedBox(
                        width: zhiSetWidth(2),
                      ),
                      Text(
                        '督办',
                        style: TextStyle(color: Colors.grey[600], fontSize: 14),
                        textAlign: TextAlign.center,
                      ),
                    ],
                  ),
                )),
                Expanded(
                    child: Container(
                  decoration: BoxDecoration(
                    border: Border(
                      right: BorderSide(color: Colors.grey[200]!, width: 1),
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Iconfont.daichuli,
                        color: Colors.grey[600],
                        size: 20,
                      ),
                      SizedBox(
                        width: zhiSetWidth(2),
                      ),
                      Text(
                        '待处理',
                        style: TextStyle(color: Colors.grey[600], fontSize: 14),
                        textAlign: TextAlign.center,
                      ),
                    ],
                  ),
                )),
                Expanded(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(
                          Iconfont.xiaoxi,
                          color: Colors.grey[600],
                          size: 20,
                        ),
                        SizedBox(
                          width: zhiSetWidth(2),
                        ),
                        Text(
                          '消息',
                          style: TextStyle(color: Colors.grey[600], fontSize: 14),
                          textAlign: TextAlign.center,
                        ),
                      ],
                    )),
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(zhiSetWidth(0), 15, zhiSetWidth(0), 15),
            child: Wrap(
              crossAxisAlignment: WrapCrossAlignment.start,
              alignment: WrapAlignment.start,
              spacing: 0,
              //主轴上子控件的间距
              runSpacing: 20,
              //交叉轴上子控件之间的间距
              children: _entryWidget(context, _list),
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 12),
            child: TabBar(
              controller: _tabController,
              isScrollable: true,
              labelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
//          labelColor: Color(0xff2e2f36),
              labelColor: AppColors.primaryElement,
              indicatorColor: Colors.transparent,
              labelPadding: EdgeInsets.fromLTRB(8, 0, 8, 0),
              unselectedLabelColor: Color(0xffb6b6b6),
              unselectedLabelStyle: TextStyle(fontSize: 16),
//              indicatorWeight: 4,
              onTap: (index) {
                _pageController.jumpToPage(index);
              },
              tabs: _tabList.map((e) => e).toList(),
            ),
          ),
          Expanded(
            child: FixTabBarView(
              pageController: _pageController,
              tabController: _tabController,
              children: _tabList.map((list) {
                return HoneTabPage(
                  title: list.text,
                );
              }).toList(),
            ),
          ),
        ],
      )),
    );
  }

  // _entryWidget(BuildContext context, list) {
  //   if (list == null) return null;
  //   List<Widget> items = [];
  //   list?.forEach((EntryEntity item) {
  //     items.add(Container(
  //       width: MediaQuery.of(context).size.width / 5 - zhiSetWidth(4),
  //       child: Column(
  //         children: <Widget>[
  //           loadAssetImage(
  //             'channel-nbc',
  //             width: 40,
  //             height: 40,
  //           ),
  //           SizedBox(
  //             height: 5,
  //           ),
  //           Text(
  //             item.title,
  //             style: TextStyle(fontSize: 14),
  //           )
  //         ],
  //       ),
  //     ));
  //   });
  //
  //   return items;
  // }
  _entryWidget(BuildContext context, list) {
    if (list == null) return null;
    List<Widget> items = [];
    list?.forEach((EntryEntity item) {
      items.add(Container(
        width: MediaQuery.of(context).size.width / 5 - zhiSetWidth(2),
        child: Column(
          children: <Widget>[
            Stack(
              alignment: Alignment.center,
              children: [
                Container(
                  decoration: new BoxDecoration(
                    //背景
                    color: item.color,
                    //设置四周圆角 角度
                    borderRadius: BorderRadius.all(Radius.circular(6.0)),
                  ),
                  width: 40,
                  height: 40,
                ),
                Center(
                  child: item.icon,
                ),
              ],
            ),
            SizedBox(
              height: 5,
            ),
            Text(
              item.title,
              style: TextStyle(fontSize: 13, color: Colors.grey[700]),
            )
          ],
        ),
      ));
    });

    return items;
  }
}
